package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzLendingImportBatchMapper;
import com.zjdiepu.www.ibiz.dz.IDzLendingImportBatchService;
import com.zjdiepu.www.model.dz.dto.DzLendingImportBatch;
import com.zjdiepu.www.model.dz.ext.DzLendingImportBatchExt;

/**
 * 银行放款导入批次表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年08月30日 14时09分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzLendingImportBatchServiceImpl extends BaseService implements IDzLendingImportBatchService {
	public static final Logger logger = LoggerFactory.getLogger(DzLendingImportBatchServiceImpl.class);

	@Autowired
	private DzLendingImportBatchMapper dzLendingImportBatchMapper;
	
	@Override
	public void saveUpdateAndDelete(DzLendingImportBatch dzLendingImportBatch, OperateType operType) {
		switch (operType) {
		case add:
			dzLendingImportBatch.initInsertData(dzLendingImportBatch.getModifier(), dzLendingImportBatch.getModifierName());
			dzLendingImportBatchMapper.insert(dzLendingImportBatch);
			break;
		case edit:
			dzLendingImportBatch.initUpdateData(dzLendingImportBatch.getModifier(), dzLendingImportBatch.getModifierName());
			// 影响的记录数
			int updateCount = dzLendingImportBatchMapper.updateByPrimaryKeySelective(dzLendingImportBatch);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzLendingImportBatch.initDeleteData(dzLendingImportBatch.getModifier(), dzLendingImportBatch.getModifierName());
			// 影响的记录数
			int deleteCount = dzLendingImportBatchMapper.updateByPrimaryKeySelective(dzLendingImportBatch);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzLendingImportBatch> selectByExt(DzLendingImportBatchExt dzLendingImportBatchExt) {
		return dzLendingImportBatchMapper.selectByExt(dzLendingImportBatchExt);
	}
	
	@Override
	public int countByExt(DzLendingImportBatchExt dzLendingImportBatchExt){
		return dzLendingImportBatchMapper.countByExt(dzLendingImportBatchExt);
	}
	
	@Override
	public DzLendingImportBatch selectById(String id) {
		return dzLendingImportBatchMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzLendingImportBatchMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzLendingImportBatch dzLendingImportBatch) {
		return dzLendingImportBatchMapper.countRefined(dzLendingImportBatch);
	}

	@Override
	public DzLendingImportBatch selectRefined(DzLendingImportBatch dzLendingImportBatch) {
		return dzLendingImportBatchMapper.selectRefined(dzLendingImportBatch);
	}
	
	@Override
	public int deletedRefined(DzLendingImportBatch dzLendingImportBatch) {
		return dzLendingImportBatchMapper.deletedRefined(dzLendingImportBatch);
	}

	@Override
	public boolean repeat(DzLendingImportBatch dzLendingImportBatch) {
		int count = countRefined(dzLendingImportBatch);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzLendingImportBatch.getId())) {
			return true;
		}
		DzLendingImportBatch dbItem = selectRefined(dzLendingImportBatch);
		if(null == dbItem) {
			return false;
		}
		return !dzLendingImportBatch.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzLendingImportBatchMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DzLendingImportBatch> dzLendingImportBatchs) {
		return dzLendingImportBatchMapper.insertBatch(dzLendingImportBatchs);
	}
}
